<?php
namespace Crm\Api;

use PhalApi\Api;
use Auth\Api\Token as Token;
use Crm\Domain\Mall as DomainMall;

/**
 * 商城服务类
 *
 * @author: luodong <751450467@qq.com> 2020-06-24
 */

class Mall extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

	public function getRules() {
        return array(
            'initialize' => array('mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')),
            'config' => array('mall_id'    => array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')),
            'setting' => array(
                'mall_id' 	=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'type' 	=> array('name' => 'type', 'type'=>'string','default'=>'list','require'=>false,'desc'=>'返回格式')
            ),
            'awardList'   => array('mall_id' 	=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')),
            'awardDetail' => array('award_id' 	=> array('name' => 'award_id', 'type'=>'int','require'=>true,'desc'=>'奖品ID')),
            'awardConvert'=> array(
                'award_id' 	=> array('name' => 'award_id', 'type'=>'int','require'=>true,'desc'=>'奖品ID'),
                'mall_id' 	=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')
            ),
        );
	}

    /**
     * 初始化商城页面
     * @desc 根据商城id,初始化商城页面
     * @return obj data 商城页面配置信息
     * @return int time 当前时间戳
     */
    public function initialize(){
        $domain = new DomainMall();
        $data['swiper'] = $domain->setting($this->mall_id,array('code'=>'2020404','alias'=>'mall_img'));
        $data['setting']= $domain->setting($this->mall_id,array('code'=>'2020404','alias'=>'mall_title'),'one');
        return $data;
    }

    /**
     * 获取商城基础配置信息
     * @desc 根据商城id,获取默认的基础配置信息
     * @return int config 商城基础配置信息
     * @return int time 当前时间戳
     */
    public function config() {
        $domain = new DomainMall();
        $rs = $domain->config($this->mall_id);
        return $rs;
    }

    /**
     * 获取配置信息
     * @desc 获取配置信息
     * @return obj setting 配置信息
     * @return int time 当前时间戳
     */
    public function setting(){
        $domain = new DomainMall();
        $param = \PhalApi\DI()->request->getAll();
        $data= $domain->setting($this->mall_id,$param,$this->type);
        return $data;
    }

    /**
     * 获取礼品列表
     * @desc 获取商城礼品列表
     * @return obj awardList 商城礼品列表
     * @return int time 当前时间戳
     */
    public function awardList(){
        $domain = new DomainMall();
        $param = \PhalApi\DI()->request->getAll();
        $data= $domain->awardList($this->mall_id,$param);
        return $data;
    }

    /**
     * 获取礼品详情
     * @desc 获取礼品详情
     * @return obj awardDetail 礼品详情
     * @return int time 当前时间戳
     */
    public function awardDetail(){
        $domain = new DomainMall();
        $data= $domain->awardDetail($this->award_id);
        return $data;
    }

    /**
     * 礼品兑换
     * @desc 礼品兑换
     * @return obj awardConvert 礼品兑换结果
     * @return int time 当前时间戳
     */
    public function awardConvert(){
        $this->user = $this->token->checkToken();
        $domain = new DomainMall();
        $data= $domain->awardConvert($this->user['user_id'],$this->award_id,$this->mall_id,$this->user['user_type']);
        return $data;
    }

}
